<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)
    die();

if(!CModule::IncludeModule("iblock"))
    return;

if( COption::GetOptionString("lombard", "wizard_installed", "N", WIZARD_SITE_ID) == "Y" && !WIZARD_INSTALL_DEMO_DATA ){
    $iblockCode = "ittian_lombard_info_box_".WIZARD_SITE_ID;
    $iblockType = "ittian_lombard_content";

    $rsIBlock = CIBlock::GetList(array(), array("XML_ID" => $iblockCode, "TYPE" => $iblockType));
    $iblockID = false;
    if ($arIBlock = $rsIBlock->Fetch()){
        $iblockID = $arIBlock["ID"];

        CWizardUtil::ReplaceMacros(WIZARD_SITE_PATH."_index.php", array("IBLOCK_INFO_BOX_TYPE" => $iblockType));
        CWizardUtil::ReplaceMacros(WIZARD_SITE_PATH."_index.php", array("IBLOCK_INFO_BOX_ID" => $iblockID));

        $bitrixTemplateDir = $_SERVER["DOCUMENT_ROOT"].BX_PERSONAL_ROOT."/templates/".WIZARD_TEMPLATE_ID;
        CWizardUtil::ReplaceMacros($bitrixTemplateDir."/footer.php", array("IBLOCK_INFO_BOX_TYPE" => $iblockType));
        CWizardUtil::ReplaceMacros($bitrixTemplateDir."/footer.php", array("IBLOCK_INFO_BOX_ID" => $iblockID));


    }
    return;
}

$iblockXMLFile = WIZARD_SERVICE_RELATIVE_PATH."/xml/".LANGUAGE_ID."/info_box.xml";
$iblockCode = "ittian_lombard_info_box_".WIZARD_SITE_ID;
$iblockType = "ittian_lombard_content";

$rsIBlock = CIBlock::GetList(array(), array("XML_ID" => $iblockCode, "TYPE" => $iblockType));
$iblockID = false;
if ($arIBlock = $rsIBlock->Fetch())
{
    $iblockID = $arIBlock["ID"];
    if (WIZARD_INSTALL_DEMO_DATA)
    {
        CIBlock::Delete($arIBlock["ID"]);
        $iblockID = false;
    }
}

if($iblockID == false)
{
    $permissions = Array(
        "1" => "X",
        "2" => "R"
    );
    $dbGroup = CGroup::GetList($by = "", $order = "", Array("STRING_ID" => "content_editor"));
    if($arGroup = $dbGroup -> Fetch())
    {
        $permissions[$arGroup["ID"]] = 'W';
    };
    $iblockID = WizardServices::ImportIBlockFromXML(
        $iblockXMLFile,
        "ittian_lombard_info_box",
        $iblockType,
        WIZARD_SITE_ID,
        $permissions
    );

    if ($iblockID < 1)
        return;

    //IBlock fields
    $iblock = new CIBlock;
    $arFields = Array(
        "ACTIVE" => "Y",
        "FIELDS" => array ( 'IBLOCK_SECTION' =>
            array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' => '', ),
            'ACTIVE' =>
                array ( 'IS_REQUIRED' => 'Y', 'DEFAULT_VALUE' => 'Y', ),
            'ACTIVE_FROM' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' => '=today', ),
            'ACTIVE_TO' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' => '', ),
            'SORT' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' => '', ),
            'NAME' =>
                array ( 'IS_REQUIRED' => 'Y', 'DEFAULT_VALUE' => '', ),
            'PREVIEW_PICTURE' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' =>
                    array ( 'FROM_DETAIL' => 'Y', 'SCALE' => 'Y', 'WIDTH' => '160', 'HEIGHT' => '65', 'IGNORE_ERRORS' => 'N', 'METHOD' => 'resample', 'COMPRESSION' => 95, 'DELETE_WITH_DETAIL' => 'Y', 'UPDATE_WITH_DETAIL' => 'Y', ), ),
            'PREVIEW_TEXT_TYPE' =>
                array ( 'IS_REQUIRED' => 'Y', 'DEFAULT_VALUE' => 'text', ),
            'PREVIEW_TEXT' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' => '', ),
            'DETAIL_PICTURE' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' =>
                    array ( 'SCALE' => 'N', 'WIDTH' => '', 'HEIGHT' => '', 'IGNORE_ERRORS' => 'N', 'METHOD' => 'resample', 'COMPRESSION' => 95, ), ),
            'DETAIL_TEXT_TYPE' =>
                array ( 'IS_REQUIRED' => 'Y', 'DEFAULT_VALUE' => 'text', ),
            'DETAIL_TEXT' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' => '', ),
            'XML_ID' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' => '', ),
            'CODE' =>
                array ( 'IS_REQUIRED' => 'Y', 'DEFAULT_VALUE' =>
                    array ( 'UNIQUE' => 'Y', 'TRANSLITERATION' => 'Y', 'TRANS_LEN' => 100, 'TRANS_CASE' => 'L', 'TRANS_SPACE' => '_', 'TRANS_OTHER' => '_', 'TRANS_EAT' => 'Y', 'USE_GOOGLE' => 'N', ), ),
            'TAGS' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' => '', ),
            'SECTION_NAME' =>
                array ( 'IS_REQUIRED' => 'Y', 'DEFAULT_VALUE' => '', ),
            'SECTION_PICTURE' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' =>
                    array ( 'FROM_DETAIL' => 'N', 'SCALE' => 'N', 'WIDTH' => '', 'HEIGHT' => '', 'IGNORE_ERRORS' => 'N', 'METHOD' => 'resample', 'COMPRESSION' => 95, 'DELETE_WITH_DETAIL' => 'N', 'UPDATE_WITH_DETAIL' => 'N', ), ),
            'SECTION_DESCRIPTION_TYPE' =>
                array ( 'IS_REQUIRED' => 'Y', 'DEFAULT_VALUE' => 'text', ),
            'SECTION_DESCRIPTION' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' => '', ),
            'SECTION_DETAIL_PICTURE' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' =>
                    array ( 'SCALE' => 'N', 'WIDTH' => '', 'HEIGHT' => '', 'IGNORE_ERRORS' => 'N', 'METHOD' => 'resample', 'COMPRESSION' => 95, ), ),
            'SECTION_XML_ID' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' => '', ),
            'SECTION_CODE' =>
                array ( 'IS_REQUIRED' => 'N', 'DEFAULT_VALUE' =>
                    array ( 'UNIQUE' => 'N', 'TRANSLITERATION' => 'Y', 'TRANS_LEN' => 100, 'TRANS_CASE' => 'L', 'TRANS_SPACE' => '_', 'TRANS_OTHER' => '_', 'TRANS_EAT' => 'Y', 'USE_GOOGLE' => 'N', ), ),
        ),
        "CODE" => "ittian_lombard_slide_page",
        "XML_ID" => $iblockCode,
    );

    $iblock->Update($iblockID, $arFields);
}
else
{
    $arSites = array();
    $db_res = CIBlock::GetSite($iblockID);
    while ($res = $db_res->Fetch())
        $arSites[] = $res["LID"];
    if (!in_array(WIZARD_SITE_ID, $arSites))
    {
        $arSites[] = WIZARD_SITE_ID;
        $iblock = new CIBlock;
        $iblock->Update($iblockID, array("LID" => $arSites));
    }
}
$dbSite = CSite::GetByID(WIZARD_SITE_ID);
if($arSite = $dbSite -> Fetch())
    $lang = $arSite["LANGUAGE_ID"];
if(strlen($lang) <= 0)
    $lang = "ru";

WizardServices::IncludeServiceLang("slide_page.php", $lang);

$arProperty = Array();
$dbProperty = CIBlockProperty::GetList(Array(), Array("IBLOCK_ID" => $iblockID));
while($arProp = $dbProperty->Fetch())
    $arProperty[$arProp["CODE"]] = $arProp["ID"];

//CUserOptions::SetOption("form", "form_element_".$iblockID, array (
//        'tabs' => 'edit1--#--'.GetMessage("WZD_OPTION_SLIDE_PAGE_1").'--,--ACTIVE--#--'.GetMessage("WZD_OPTION_SLIDE_PAGE_2").'--,--NAME--#--'.GetMessage("WZD_OPTION_SLIDE_PAGE_3").'--,--CODE--#--'.GetMessage("WZD_OPTION_SLIDE_PAGE_4").'--,--SORT--#--'.GetMessage("WZD_OPTION_SLIDE_PAGE_5").'--,--PROPERTY_'.$arProperty["TITLE"].'--#--'.GetMessage("WZD_OPTION_SLIDE_PAGE_PROPERTY_TITLE").'--,--PROPERTY_'.$arProperty["TEXT"].'--#--'.GetMessage("WZD_OPTION_SLIDE_PAGE_PROPERTY_TEXT").'--,--',
//    ));
//
//CUserOptions::SetOption("list", "tbl_iblock_list_".md5($iblockType.".".$iblockID), array (
//        'columns' => 'NAME,ACTIVE,DATE_ACTIVE_FROM', 'by' => 'timestamp_x', 'order' => 'desc', 'page_size' => '20',
//    ));

CWizardUtil::ReplaceMacros(WIZARD_SITE_PATH."_index.php", array("IBLOCK_INFO_BOX_TYPE" => $iblockType));
CWizardUtil::ReplaceMacros(WIZARD_SITE_PATH."_index.php", array("IBLOCK_INFO_BOX_ID" => $iblockID));

$bitrixTemplateDir = $_SERVER["DOCUMENT_ROOT"].BX_PERSONAL_ROOT."/templates/".WIZARD_TEMPLATE_ID;
CWizardUtil::ReplaceMacros($bitrixTemplateDir."/footer.php", array("IBLOCK_INFO_BOX_TYPE" => $iblockType));
CWizardUtil::ReplaceMacros($bitrixTemplateDir."/footer.php", array("IBLOCK_INFO_BOX_ID" => $iblockID));


?>